<template>
  <el-form :model="registerForm" :rules="registerFormRules" ref="registerFormRef" label-position="left" label-width="0px" class="demo-ruleForm login-container" status-icon>
    <h3 class="title">用户注册</h3>
    <el-form-item prop="account">
      <el-input type="text" v-model="registerForm.account" auto-complete="off" placeholder="账号"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input type="password" v-model="registerForm.password" auto-complete="off" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item prop="checkPassword">
      <el-input type="password" v-model="registerForm.checkPassword" auto-complete="off" placeholder="确认密码"></el-input>
    </el-form-item>
    <el-form-item style="width:100%;">
      <el-button type="primary" style="width:100%;" @click="submit(registerFormRef)">注册</el-button>
      <el-button style="width:100%;position:relative;top:10px;left:-11px" @click="resetForm(registerFormRef)">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { FormInstance, FormRules } from 'element-plus';
import { resetForm } from '../utils'
import { FormCheckFunction } from '../typing'
import { useRouter } from 'vue-router'

const router = useRouter()
const registerFormRef = ref<FormInstance>()
const registerForm = reactive({
  account: "",
  password: "",
  checkPassword: "",
})

const passwordCheck: FormCheckFunction = (rules, value, callback) => {
  if (value === "") {
    return callback(new Error("请输入确认密码！"))
  }
  if (registerForm.password != registerForm.checkPassword) {
    return callback(new Error("确认密码不一致！"))
  }
  return callback()
}

const registerFormRules = reactive<FormRules>({
  account: [
    {
      type: "string",
      required: true,
      trigger: "blur",
    }
  ],
  password: [
    {
      type: "string",
      required: true,
      trigger: "change",
      message: "请输入密码！"
    }
  ],
  checkPassword: [
    {
      type: "string",
      required: true,
      trigger: "change",
      validator: passwordCheck,
    }
  ]
})

const submit = (formEl: FormInstance | undefined): boolean => {
  router.push({name: "login"})
  return true
}

</script>

<style lang="scss" scoped>
  .login-container {
    /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
    .title {
      margin: 0px auto 40px auto;
      text-align: center;
      color: #505458;
    }
    .remember {
      margin: 0px 0px 35px 0px;
    }
  }
</style>
